//引入功能模块
const mysql = require('mysql');

//创建连接池
let pool = mysql.createPool({
    host: 'localhost',
    user: 'root',
    password: 'root',
    port: 3306,
    connectionLimit: 10,
    database: 'yinpingshop'
});

//连接数据库
const exec = (sql, param) => {
    return new Promise(function (resolve, reject) {
        pool.getConnection(function (err, conn) {
            if (err) {
                console.log('数据库连接失败:' + err);
                reject(err);
            }
            conn.query(sql, param, function (err, result) {
                if (err) {
                    console.log('sql语句执行失败:' + err);
                    reject(err);
                }
                resolve(result);
                conn.release();
            })
        })
    })
}

module.exports = {exec};